<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../../global/initialize.php");

if( isset( $_POST[ 'ruler_name' ] ) ) {
    $query = "SELECT ruler_id " .
    		"FROM ruler " .
    		"WHERE name = '" . formatInput( $_POST[ 'ruler_name' ] ) . "'";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'ruler_id' ] ) ) {
		$request_ruler_id = $row[ 'ruler_id' ]; 
	}
	else {
		$request_ruler_id = -1;
		$status = "No ruler with that name!";
	}
}
else {
	if( isset( $_POST[ 'ruler_id' ] ) ) {
	    $query = "SELECT count(*) " .
	    		"FROM ruler " .
	    		"WHERE ruler_id = '" . formatInput( $_POST[ 'ruler_id' ] ) . "'";
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( $row[ 'count(*)' ] > 0 ) {
			$request_ruler_id = formatInput( $_POST[ 'ruler_id' ] );
		}
		else {
			$request_ruler_id = -1;
			$status = "No ruler with that name!";			
		} 
	}
	else {
		$request_ruler_id = -1;
		$status = "Nothing selected!";			
	} 
}

if( $request_ruler_id != -1 ) {
	if( isset( $_POST[ 'uninvite' ] ) ) {
	    $query = "DELETE FROM faction_member " .
	    		"WHERE ruler_id = '" . $request_ruler_id . "' AND faction_id = '" . formatInput( $_POST[ 'faction_id' ] ) . "'";
	    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	}
	else {
	    $query = "SELECT count(*) " .
	    		"FROM faction_member " .
	    		"WHERE invited = 1 AND ruler_id = " . $request_ruler_id . " AND faction_id = " . formatInput( $_POST[ 'faction_id' ] );
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( $row[ 'count(*)' ] ) {
			$status = "Ruler is already invited.";
		}
		else {
		    $query = "SELECT count(*) " .
		    		"FROM faction_member " .
		    		"WHERE requested = 0 AND invited = 0 AND ruler_id = " . $request_ruler_id;
		    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( $row[ 'count(*)' ] ) {
				$status = "This ruler is already in a faction!";
			}
			else {
			    $query = "SELECT count(*) " .
			    		"FROM faction_member " .
			    		"WHERE requested = 1 AND ruler_id = " . $request_ruler_id . " AND faction_id = " . formatInput( $_POST[ 'faction_id' ] );
			    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				$already_requested = false;
				if( $row[ 'count(*)' ] > 0 ) {
					$already_requested = true;
				}
				
				$query = "SELECT village_id FROM village WHERE ruler_id = " . $request_ruler_id . " AND start_place = 1";
			    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				
				if( $already_requested || checkRangeVillage( $row[ 'village_id' ] ) ) {
				    $query = "SELECT count(*) FROM faction_member WHERE faction_id = " . formatInput( $_POST[ 'faction_id' ] ) . " AND requested = 0";
				    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					$members = $row[ 'count(*)' ];
				
				    $query = "SELECT max_members FROM faction_types JOIN faction on faction.faction_type_id = faction_types.faction_type_id WHERE faction_id = " . formatInput( $_POST[ 'faction_id' ] );
				    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					
					if( $row[ 'max_members' ] <= $members ) {
						$status = "Your faction is already at it's maximum size(invites included)!";
					}
					else {
					    $query = "SELECT count(*) FROM faction_member WHERE ruler_id = " . $request_ruler_id . " AND requested = 1 AND faction_id = " . formatInput( $_POST[ 'faction_id' ] );
					    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$row = mysql_fetch_array( $result, MYSQL_ASSOC );
						if( $row[ 'count(*)' ] > 0 ) {
						    $query = "DELETE FROM faction_member WHERE ruler_id = " . $request_ruler_id;
						    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							
						    $query = "INSERT INTO faction_member ( ruler_id, faction_id ) VALUES( " . $request_ruler_id . ", " . formatInput( $_POST[ 'faction_id' ] ) . " )";
						    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						}
						else {
						    $query = "INSERT INTO faction_member ( ruler_id, faction_id, invited ) VALUES( " . $request_ruler_id . ", " . formatInput( $_POST[ 'faction_id' ] ) . ", 1 )";
						    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						}
					}
				}
				else {
					$status = "This ruler is out of range. <small>(You need to improve your communications to invite this ruler)</small>";
				}
			}
		}
	}
}

if( isset( $status ) ) {
	$_SESSION[ 'member_invite_status' ] = $status;
}
header( "Location: ../invite_list.php" );
?>
